/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bld.util.facade;

import br.com.bld.util.conexao.ConexaoPGSQL;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Pedro
 */
public class EntityFactory {

    private final String schema;
    private final String table;
    private final String[] fields;
    private String[] pk;
    private String sequence;
    private final Connection conn;

    private static final Properties props = new Properties();
    private static final Logger logger = Logger.getLogger(ConexaoPGSQL.class.getName());

    static {

        try {
            InputStream file = ConexaoPGSQL.class.getResourceAsStream("bld.properties");
            props.load(file);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, ex.toString(), ex);
        }
    }

    public EntityFactory(Connection conn, String table, String[] fields, String[] pk, String sequence) {
        this.conn = conn;
        this.schema = props.getProperty("schema");
        this.table = table;
        this.fields = fields;
        this.pk = pk;
        this.sequence = sequence;
    }

    public EntityFactory(Connection conn, String table, String[] fields, String[] pk) {
        this.conn = conn;
        this.schema = props.getProperty("schema");
        this.table = table;
        this.fields = fields;
        this.pk = pk;
    }

    public EntityFactory(Connection conn, String schema, String table, String[] fields) {
        this.conn = conn;
        this.schema = props.getProperty("schema");
        this.table = table;
        this.fields = fields;
    }

    public EntityFactory(Connection conn, String schema, String table, String[] fields, String sequence) {
        this.conn = conn;
        this.schema = props.getProperty("schema");
        this.table = table;
        this.fields = fields;
        this.sequence = sequence;
    }

    /**
     * @return the schema
     */
    public String getSchema() {
        return schema;
    }

    /**
     * @return the table
     */
    public String getTable() {
        return table;
    }

    /**
     * @return the fields
     */
    public String[] getFields() {
        return fields;
    }

    /**
     *
     * @return
     */
    public String getFieldsFormatatedToInsert() {

        StringBuilder str = new StringBuilder();

        for (String field : fields) {
            str.append(field).append(", ");
        }

        return str.toString().substring(0, str.length() - 2);
    }

    public String getPKFormatatedToWhere() {

        StringBuilder str = new StringBuilder();

        for (String key : pk) {
            str.append(key).append(" = ?, ");
        }

        return str.toString().substring(0, str.length() - 2);
    }

    public String getOrderByAsc() {

        StringBuilder str = new StringBuilder();

        for (String key : pk) {
            str.append(key).append(" ASC, ");
        }

        return str.toString().substring(0, str.length() - 2);
    }

    /**
     *
     * @return
     */
    public String getFieldsFormatatedToUpdate() {

        StringBuilder str = new StringBuilder();

        for (String key : pk) {
            for (String field : fields) {
                if (!key.equals(field)) {
                    str.append(field).append(" = ?, ");
                }
            }
        }

        return str.toString().substring(0, str.length() - 2);
    }

    /**
     *
     * @return
     */
    public String getFieldsParm() {
        StringBuilder str = new StringBuilder();

        for (String field : fields) {
            str.append("?").append(", ");
        }

        return str.toString().substring(0, str.length() - 2);
    }

    public String getWhereFields() {
        StringBuilder str = new StringBuilder();

        for (String key : pk) {
            for (String field : fields) {
                if (!key.equals(field)) {
                    str.append(field).append(" = ? AND \n");
                }
            }
        }

        return str.toString().substring(0, str.length() - 5);
    }

    /**
     * @return the pk
     */
    public String[] getPk() {
        return pk;
    }

    /**
     * @return the sequence
     */
    public String getSequence() {
        return sequence;
    }
}
